home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util4 / aman-115.lha / aman-1.15 / Source / main.c < prev    next >
C/C++ Source or Header  |  1995-10-04  |  9KB  |  317 lines

  1. /*                                                               -*- C -*-
  2.  *  MAIN.C
  3.  *
  4.  *  (c)Copyright 1994,95 by Tobias Ferber,  ferber@rpk.mach.uni-karlsruhe.de
  5.  *
  6.  *  This file is part of the AMan distribution
  7.  *
  8.  *  AMan is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published
  10.  *  by the Free Software Foundation; either version 1 of the License,
  11.  *  or (at your option) any later version.
  12.  *
  13.  *  AMan is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with this program; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. /* $VER: $Id: main.c,v 1.3 1995/09/15 21:00:59 tf Exp $ */
  24.  
  25. #include "version.h"
  26.  
  27. #include <clib/muimaster_protos.h>
  28. #include <clib/alib_protos.h>
  29. #include <clib/dos_protos.h>
  30. #include <clib/exec_protos.h>
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34.  
  35. #include "aman.h"
  36.  
  37. extern BOOL MatchPatternNoCase(STRPTR, STRPTR);
  38.  
  39. struct Library *MUIMasterBase;
  40.  
  41. /*
  42.  *  Insert all docs matching `expr' into the listview `lv'
  43.  */
  44.  
  45. static int fdb(APTR lv, char *negpat, char *dbase, long exact, char *expr)
  46. {
  47.   int err= 0;
  48.   char strbuf[1024],fname[80];
  49.  
  50.   sprintf(fname,"T:AMan.%ld",(long)FindTask(NULL));
  51.   sprintf(strbuf,"%s NH F DB \"%s\" P \"%s\" > \"%s\"", exact ? "FDB E" : "FDB", dbase,expr,fname);
  52.  
  53. #ifdef DEBUG
  54.   printf("system(%s)\n",strbuf);
  55. #endif
  56.  
  57.   if( (err= system(strbuf)) == 0 )
  58.   {
  59.     FILE *fp= fopen(fname,"r");
  60.  
  61.     if(fp)
  62.     {
  63.       while( !feof(fp) )
  64.       {
  65.         int len= strlen( fgets(strbuf,sizeof(strbuf),fp) ) - 1;
  66.         strbuf[(len >= 0) ? len : 0]= '\0';
  67.  
  68.         if( !feof(fp) && strbuf[0] && (!negpat || (negpat && !MatchPatternNoCase(negpat,strbuf))) )
  69.           DoMethod( lv, MUIM_List_InsertSingle, strbuf, MUIV_List_Insert_Sorted );
  70.       }
  71.       fclose(fp);
  72.     }
  73.     else err= 1;
  74.   }
  75.  
  76.   return err + unlink(fname);
  77. }
  78.  
  79. /**/
  80.  
  81. struct RDArgs *StrReadArgs(STRPTR template, LONG *array, STRPTR str)
  82. {
  83.   static struct RDArgs rdargs;
  84.  
  85.   rdargs.RDA_Source.CS_Buffer = str;
  86.   rdargs.RDA_Source.CS_Length = strlen(str);
  87.   rdargs.RDA_Source.CS_CurChr = 0;
  88.   rdargs.RDA_DAList = 0;
  89.   rdargs.RDA_Buffer = NULL;
  90.   rdargs.RDA_BufSiz = 0;
  91.   rdargs.RDA_ExtHelp = NULL;
  92.   rdargs.RDA_Flags = RDAF_NOPROMPT;
  93.  
  94.   return ReadArgs(template,array,&rdargs);
  95. }
  96.  
  97. /**/
  98.  
  99. int aman(char *fname)
  100. {
  101.   int done= 0;
  102.   char strbuf[1024];
  103.  
  104.   FILE *fp= fopen("ENV:AMan.config","r");
  105.  
  106.   if(fp)
  107.   {
  108.     while( !done && !feof(fp) )
  109.     {
  110.       int len= strlen( fgets(strbuf,sizeof(strbuf),fp) ) - 1;
  111.       strbuf[(len >= 0) ? len : 0]= '\0';
  112.  
  113.       if( !feof(fp) && strbuf[0] )
  114.       {
  115.         long args[2]= { 0,0 };
  116.         struct RDArgs *a;
  117.  
  118.         if( (a= StrReadArgs("PATTERN/A,COMMAND/A",args,strbuf)) )
  119.         {
  120.           char pattern[80];
  121.           ParsePatternNoCase( (char *)(args[0]), pattern, sizeof(pattern) );
  122.  
  123.           if( MatchPatternNoCase(pattern,fname) )
  124.           {
  125.             sprintf(strbuf,(char *)args[1],fname);
  126.             done= 1;
  127.           }
  128.           FreeArgs(a);
  129.         }
  130.         /*else done= -1;*/  /* !StrReadArgs() */
  131.       }
  132.     }
  133.     fclose(fp);
  134.   }
  135.  
  136.   if(!done)
  137.     sprintf(strbuf,"Run <NIL: >NIL: MultiView \"%s\"",fname);
  138.  
  139.   return (done < 0) ? 1 : system(strbuf);
  140. }
  141.  
  142. /**/
  143.  
  144. int newargs(struct ObjApp *app, LONG *args)
  145. {
  146.   int err= RETURN_OK;
  147.  
  148.   char *str= (char *)(args[1]);
  149.   char *pat= (char *)0;
  150.  
  151.   char *db= args[0] ? (char *)(args[0]) : "aman";
  152.  
  153.   if(str)
  154.   {
  155.     size_t len= 2*strlen(str)+2;
  156.  
  157.     if( (pat= (char *)malloc(len)) )
  158.       ParsePatternNoCase( str, pat, len );
  159.     else
  160.       err= ERROR_NO_FREE_STORE;
  161.   }
  162.  
  163.   if(err == RETURN_OK)
  164.   {
  165.     char **expr= (char **)(args[3]);
  166.  
  167.     if(expr) while(*expr)
  168.       fdb( app->LV_aman, pat, db, args[2], *expr++ );
  169.  
  170.     set( app->LV_aman, MUIA_List_Active, MUIV_List_Active_Top );
  171.     set( app->WI_aman, MUIA_Window_ActiveObject, app->LV_aman );
  172.   }
  173.  
  174.   if(pat)
  175.     free(pat);
  176.  
  177.   return err;
  178. }
  179.  
  180. /**/
  181.  
  182. int main(int argc, char *argv[])
  183. {
  184.   static char *template= "DB=DataBase/K,H=Hide/K,E=Exact/S,P=Pattern/A/M";
  185.   struct RDArgs *a= (struct RDArgs *)0;
  186.   LONG args[4] = { 0,0,0,0 };  /* args array */
  187.   int rc= RETURN_OK;
  188.  
  189.   if( (argc==0) || (a= ReadArgs(template, args, NULL)) )
  190.   {
  191.     if( (MUIMasterBase = OpenLibrary(MUIMASTER_NAME, MUIMASTER_VMIN)) )
  192.     {
  193.       struct ObjApp *app;
  194.  
  195.       BOOL running;
  196.       ULONG signal;
  197.  
  198.       if( (running= (app= CreateApp()) ? TRUE : FALSE) )
  199.       {
  200.         if(argc > 0) running= ( (rc= newargs(app,args)) == RETURN_OK ) ? TRUE : FALSE;
  201.         else set( app->WI_aman, MUIA_Window_ActiveObject, app->STR_pattern );
  202.  
  203.         while(running)
  204.         {
  205.           switch( DoMethod(app->App, MUIM_Application_Input, &signal) )
  206.           {
  207.             case MUIV_Application_ReturnID_Quit:
  208.               running = 0;
  209.               break;
  210.  
  211.             case ID_SELECT:
  212.               set( app->WI_aman, MUIA_Window_Sleep, TRUE);
  213.               {
  214.                 char *fname;
  215.                 DoMethod( app->LV_aman, MUIM_List_GetEntry, MUIV_List_GetEntry_Active, &fname );
  216.                 running= aman(fname) ? FALSE : TRUE;
  217.               }
  218.               set( app->WI_aman, MUIA_Window_Sleep, FALSE);
  219.               break;
  220.  
  221.             case ID_SUBJECT:
  222.                set( app->WI_aman, MUIA_Window_Sleep, TRUE);
  223.                {
  224.                  char *str, *buf;
  225.                  get( app->STR_pattern, MUIA_String_Contents, &str );
  226.  
  227.                  if(str && *str)
  228.                  {
  229.                    if( (buf= (char *)malloc(strlen(str)+2)) )
  230.                    {
  231.                      sprintf(buf,"%s ",str);
  232.                      FreeArgs(a);
  233.                      if( (a= StrReadArgs(template, args, buf)) )
  234.                        running= ( (rc= newargs(app,args)) == RETURN_OK ) ? TRUE : FALSE;
  235.                      /*else PrintFault(rc= IoErr(), "Subject"), running= FALSE;*/
  236.                      free(buf);
  237.                    }
  238.                    else rc= ERROR_NO_FREE_STORE, running= FALSE;
  239.                  }
  240.                }
  241.                set( app->WI_aman, MUIA_Window_Sleep, FALSE);
  242.                break;
  243.  
  244.             case ID_ABOUT:
  245.               MUI_Request(app->App, app->WI_aman, 0, NULL, "OK", MUIX_C
  246.                 "AMan (compiled " __DATE__ ", " __TIME__ ")\n"
  247.                 "Copyright ©1994,95 by Tobias Ferber, ferber@rpk.mach.uni-karlsruhe.de\n"
  248.                 "\nThis is a MUI-Application.\n"
  249.                 "MUI is copyrighted by Stefan Stuntz.",
  250.                 TAG_END);
  251.               break;
  252.           }
  253.  
  254.           if(running && signal)
  255.             Wait(signal);
  256.         }
  257.  
  258.         DisposeApp(app);
  259.       }
  260.  
  261.       CloseLibrary(MUIMasterBase);
  262.     }
  263.     else /* !MUIMasterBase */
  264.     {
  265.       fprintf(stderr,"You need %s V%ld+\n",MUIMASTER_NAME,MUIMASTER_VMIN);
  266.       rc= RETURN_FAIL;
  267.     }
  268.  
  269.     if(a)
  270.       FreeArgs(a);
  271.   }
  272.   else /* !ReadArgs() */
  273.   {
  274.     if(argc == 1)
  275.       puts( "AMan Version " VERSION " (compiled " __DATE__ ", " __TIME__ ")\n"
  276.             "(c)Copyright 1994,95 by Tobias Ferber, ferber@rpk.mach.uni-karlsruhe.de\n"
  277.             "\n"
  278.             "AMan is free software; you can redistribute it and/or modify\n"
  279.             "it under the terms of the GNU General Public License as published\n"
  280.             "by the Free Software Foundation; either version 2 of the License,\n"
  281.             "or (at your option) any later version.\n"
  282.             "\n"
  283.             "AMan is distributed in the hope that it will be useful,\n"
  284.             "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  285.             "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n"
  286.             "GNU General Public License for more details.\n"
  287.             "\n"
  288.             "You should have received a copy of the GNU General Public License\n"
  289.             "along with this program; see the file COPYING.  If not, write to the\n"
  290.             "Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.\n" );
  291.     else
  292.       PrintFault(rc= IoErr(), NULL);
  293.   }
  294.  
  295.   return rc;
  296. }      
  297.  
  298.  
  299. #ifdef _DCC
  300. int brkfunc(void) { return(0); }
  301.  
  302. int wbmain(struct WBStartup *wb_startup)
  303. {
  304.   /*extern int main(int argc, char *argv[]);*/
  305.   return (main(0, (char **)wb_startup));
  306. }
  307. #endif /* _DCC */
  308.  
  309.  
  310. #ifdef __SASC
  311. int CXBRK(void) { return(0); }
  312. int _CXBRK(void) { return(0); }
  313. void chkabort(void) {}
  314. #endif /* __SASC */
  315.  
  316. LONG __stack = 8192;
  317.